package xcmg.device.web.agentPlan;

import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.enums.AgentPlanStatusEnum;
import xcmg.device.service.agentPlan.AgentPlanService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.agentPlan.AgentPlanAddVO;
import xcmg.device.service.vo.agentPlan.AgentPlanSelectVO;
import xcmg.device.service.vo.agentPlan.AgentPlanVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

/**
 * @author wangyong
 */
@RestController
@RequestMapping("/agentPlan")
@Slf4j
public class AgentPlanController extends BasicController {

  @Autowired
  private AgentPlanService agentPlanService;

  @RequestMapping(value = "/storeList", method = RequestMethod.POST)
  @ApiOperation("门店采购计划列表")
  @FastMappingInfo(needLogin = true)
  public ActionResult storeList(@RequestBody SearchCommonVO<AgentPlanVO> condition)
      throws Exception {

    condition.getFilters().setOrgId(getOrgId());
    PageCommonVO pageCommonVO = agentPlanService.list(condition, AgentPlanVO.PLAN_TYPE_STORE);
    return actionValueResult(pageCommonVO.getPageInfo());
  }

  @RequestMapping(value = "/summaryList", method = RequestMethod.POST)
  @ApiOperation("汇总采购计划列表")
  @FastMappingInfo(needLogin = true)
  public ActionResult summaryList(@RequestBody SearchCommonVO<AgentPlanVO> condition)
      throws Exception {
    condition.getFilters().setOrgId(getOrgId());
    PageCommonVO pageCommonVO = agentPlanService.list(condition, AgentPlanVO.PLAN_TYPE_SUMMARY);
    return actionValueResult(pageCommonVO.getPageInfo());
  }

  @RequestMapping(value = "/storeSave", method = RequestMethod.POST)
  @ApiOperation("保存门店采购计划")
  @FastMappingInfo(needLogin = true)
  public ActionResult storeSave(@RequestBody AgentPlanAddVO addVO) throws Exception {

    try {
      addVO.getHeaderInfo().setPlanType(AgentPlanVO.PLAN_TYPE_STORE);
      agentPlanService.save(addVO, getUserId(), getOrgId(), AgentPlanStatusEnum.DRAFT.getStatus());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }

    return actionResult(ErrorCode.Success);
  }

  @RequestMapping(value = "/storeCommit", method = RequestMethod.POST)
  @ApiOperation("提交门店采购计划")
  @FastMappingInfo(needLogin = true)
  public ActionResult storeCommit(@RequestBody AgentPlanAddVO addVO) throws Exception {

    try {
      addVO.getHeaderInfo().setPlanType(AgentPlanVO.PLAN_TYPE_STORE);
      agentPlanService
          .save(addVO, getUserId(), getOrgId(), AgentPlanStatusEnum.WAIT_AUDIT.getStatus());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionResult(ErrorCode.Success);
  }

  @RequestMapping(value = "/summarySave", method = RequestMethod.POST)
  @ApiOperation("保存汇总的采购计划")
  @FastMappingInfo(needLogin = true)
  public ActionResult summarySave(@RequestBody AgentPlanAddVO addVO) throws Exception {

    try {
      addVO.getHeaderInfo().setPlanType(AgentPlanVO.PLAN_TYPE_SUMMARY);
      agentPlanService.save(addVO, getUserId(), getOrgId(), AgentPlanStatusEnum.DRAFT.getStatus());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }

    return actionResult(ErrorCode.Success);
  }

  @RequestMapping(value = "/summaryCommit", method = RequestMethod.POST)
  @ApiOperation("提交汇总的采购计划")
  @FastMappingInfo(needLogin = true)
  public ActionResult summaryCommit(@RequestBody AgentPlanAddVO addVO) throws Exception {

    try {
      addVO.getHeaderInfo().setPlanType(AgentPlanVO.PLAN_TYPE_SUMMARY);
      agentPlanService
          .save(addVO, getUserId(), getOrgId(), AgentPlanStatusEnum.WAIT_AUDIT.getStatus());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionResult(ErrorCode.Success);
  }


  @RequestMapping(value = "/item", method = RequestMethod.GET)
  @ApiOperation("查询采购计划详情")
  @FastMappingInfo(needLogin = true)
  public ActionResult item(String id) {
    return actionValueResult(agentPlanService.item(id));
  }

  @RequestMapping(value = "/updateSave", method = RequestMethod.POST)
  @ApiOperation("修改保存采购计划")
  @FastMappingInfo(needLogin = true)
  public ActionResult updateSave(@RequestBody AgentPlanAddVO addVO) {
    try {
      agentPlanService
          .update(addVO, getUserId(), getOrgId(), AgentPlanStatusEnum.DRAFT.getStatus());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionResult(ErrorCode.Success);
  }

  @RequestMapping(value = "/updateCommit", method = RequestMethod.POST)
  @ApiOperation("修改提交采购计划")
  @FastMappingInfo(needLogin = true)
  public ActionResult updateCommit(@RequestBody AgentPlanAddVO addVO) {
    try {
      agentPlanService
          .update(addVO, getUserId(), getOrgId(), AgentPlanStatusEnum.WAIT_AUDIT.getStatus());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionResult(ErrorCode.Success);
  }

  @RequestMapping(value = "/auditPass", method = RequestMethod.POST)
  @ApiOperation("审核采购计划-审核通过")
  @FastMappingInfo(needLogin = true)
  public ActionResult auditPass(@RequestBody AgentPlanVO agentPlanVO) {

    try {
      agentPlanService
          .audit(agentPlanVO, getUserId(), AgentPlanStatusEnum.AUDIT_PASSED.getStatus());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionResult(ErrorCode.Success);
  }

  @RequestMapping(value = "/auditReturn", method = RequestMethod.POST)
  @ApiOperation("审核采购计划-返回修改")
  @FastMappingInfo(needLogin = true)
  public ActionResult auditReturn(@RequestBody AgentPlanVO agentPlanVO) throws Exception {
    try {
      agentPlanService
          .audit(agentPlanVO, getUserId(), AgentPlanStatusEnum.RETURN_UPDATE.getStatus());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionResult(ErrorCode.Success);
  }

  @RequestMapping(value = "/getSummaryResult", method = RequestMethod.POST)
  @ApiOperation("返回汇总结果")
  @FastMappingInfo(needLogin = true)
  public ActionResult getSummaryResult(@RequestBody AgentPlanSelectVO selectVO) {
    AgentPlanAddVO planAddVO;
    try {
      planAddVO = agentPlanService.getSummaryResult(selectVO, getUserId(), getOrgId());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionValueResult(planAddVO);
  }

  @RequestMapping(value = "/delete", method = RequestMethod.GET)
  @ApiOperation("删除采购计划")
  @FastMappingInfo(needLogin = true)
  public ActionResult delete(String id) {
    try {
      agentPlanService.delete(id, getUserId());
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionResult(ErrorCode.Success);
  }

  @RequestMapping(value = "/genOrderList", method = RequestMethod.POST)
  @ApiOperation("汇总采购计划-选择门店采购计划列表")
  @FastMappingInfo(needLogin = true)
  public ActionResult list4Summary(@RequestBody SearchCommonVO<AgentPlanVO> condition) {
    PageCommonVO pageCommonVO;
    try {
      condition.getFilters().setOrgId(getOrgId());
      pageCommonVO = agentPlanService.list4Summary(condition);
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionValueResult(pageCommonVO);
  }

  @RequestMapping(value = "/list4PurchaseRequest", method = RequestMethod.POST)
  @ApiOperation("新增采购订单-选择汇总后采购计划列表")
  @FastMappingInfo(needLogin = true)
  public ActionResult list4PurchaseRequest(@RequestBody SearchCommonVO<AgentPlanVO> condition) {
    PageCommonVO pageCommonVO;
    try {
      condition.getFilters().setOrgId(getOrgId());
      pageCommonVO = agentPlanService.list4PurchaseRequest(condition);
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionValueResult(pageCommonVO);
  }

  @RequestMapping(value = "/select4PurchaseOrder", method = RequestMethod.GET)
  @ApiOperation("新增采购订单-选择采购计划")
  @FastMappingInfo(needLogin = true)
  public ActionResult select4PurchaseOrder(@RequestParam(value = "planId",required = false) String planId,@RequestParam(value = "dealerOrgId",required = false) String dealerOrgId) {
    AgentPlanAddVO addVO;
    try {
      addVO = agentPlanService.select4PurchaseOrder(planId, getOrgId(),dealerOrgId);
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionValueResult(addVO);
  }

  @RequestMapping(value = "/storeExport", method = RequestMethod.POST)
  @ApiOperation("门店采购计划导出")
  @FastMappingInfo(needLogin = true)
  public ActionResult storeExport(AgentPlanVO condition) {

    try {
      condition.setOrgId(getOrgId());
      condition.setPlanType(AgentPlanVO.PLAN_TYPE_STORE);
      agentPlanService.export(condition);
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionResult(ErrorCode.Success);
  }

  @RequestMapping(value = "/summaryExport", method = RequestMethod.POST)
  @ApiOperation("汇总采购计划导出")
  @FastMappingInfo(needLogin = true)
  public ActionResult summaryExport(AgentPlanVO condition) {

    try {
      condition.setPlanType(AgentPlanVO.PLAN_TYPE_SUMMARY);
      condition.setOrgId(getOrgId());
      agentPlanService.export(condition);
    } catch (Exception e) {
      return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
    }
    return actionResult(ErrorCode.Success);
  }

  @ResponseBody
  @RequestMapping(value = "/import", method = RequestMethod.POST)
  @ApiOperation("导入采购计划")
  @FastMappingInfo(needLogin = true)
  public ActionResult importExcel(HttpServletRequest request) {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
    String fileName = file.getOriginalFilename();
    FunctionResult<String> result = new FunctionResult<>();
    try {
      if (!fileName.trim().endsWith(".xls") && !fileName.trim().endsWith(".xlsx")) {
        throw new Exception("文件格式错误");
      } else {
        result = agentPlanService.importExcel(file, getOrgId(), getUserId());
      }
    } catch (Exception e) {
      log.error("导入门店采购计划异常{}",e);
      return actionResult(ErrorCode.IllegalArument, e.getMessage());
    }
    return actionResult(result.getCode(), result.getValue());
  }
}
